package com.isoft.filelistener.flistener;


import com.isoft.filelistener.Utils.ApplicationContextProvider;
import com.isoft.filelistener.Utils.XmlUtils;
import com.isoft.filelistener.dao.DfmeLbdeDao;
import com.isoft.filelistener.dao.DfmeOnreDao;
import com.isoft.filelistener.entity.*;
import com.isoft.filelistener.service.DfmePokeService;
import com.isoft.filelistener.service.DfmeStlsService;
import com.isoft.filelistener.service.DfoeDfdlService;
import com.isoft.filelistener.service.DfoeDfieService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.SimpleDateFormat;

@Component
@Slf4j
public class  FileListenerf implements FileAlterationListener {
    private String driction=null;
    @Override
    public void onDirectoryChange(File file) {

    }

    @Override
    public void onDirectoryCreate(File file) {
        log.info(file.getName()+"创建文件夹");
        driction=file.getName();
    }

    @Override
    public void onDirectoryDelete(File file) {
        log.info(file.getName()+"文件夹删除");
    }

    @Override
    public void onFileChange(File file) {

    }

    @Override
    public void onFileCreate(File file) {
        String name = file.getName();

        switch (name.substring(0,9)){
            case "DFME-POKE":
                try {
                    InputStream inputStream=new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    log.info(inputStream+"");
                    log.info(driction);
                    DfmePoke dfmePoke=XmlUtils.parseXml(inputStream, DfmePoke.class);
                    log.info(dfmePoke+"");
                    ApplicationContextProvider.getBean(DfmePokeService.class).insertPoke(dfmePoke.getMeta(),dfmePoke.getDflt());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-STLS":
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
                    String id =sdf.format(System.currentTimeMillis());
                    InputStream inputStream=new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    DfmeStls dfmeStls=XmlUtils.parseXml(inputStream, DfmeStls.class);
                    //meta头部插入语句
                    ApplicationContextProvider.getBean(DfmeStlsService.class).InsertStlsMeta(dfmeStls.getMetaData(),id);
                    //插入dflt
                    ApplicationContextProvider.getBean(DfmeStlsService.class).insertDflt(dfmeStls.getDflt(),id);
                    //插入stls
                    for (int i = 0; i < dfmeStls.getDflt().getStnd().size(); i++) {
                        DfmeStlsStnd dfmeStlsStnd = dfmeStls.getDflt().getStnd().get(i);
                        ApplicationContextProvider.getBean(DfmeStlsService.class).insertStls(dfmeStlsStnd,id);
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFOE-DFIE":
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
                    String id =sdf.format(System.currentTimeMillis());
                    InputStream inputStream=new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    DfoeDfie dfoeDfie= XmlUtils.parseXml(inputStream, DfoeDfie.class);
                    //meta插入语句
                    ApplicationContextProvider.getBean(DfoeDfieService.class).InsertDfoeMeta(dfoeDfie.getMetaData(),id);
                    //arpt插入语句
                    DfoeDfieDfltArpt dfoeDfieDfltArpt1 = dfoeDfie.getDflt().getArpt().get(0);
                    DfoeDfieDfltArpt dfoeDfieDfltArpt2 = dfoeDfie.getDflt().getArpt().get(1);
                    ApplicationContextProvider.getBean(DfoeDfieService.class).InsertDfoeArpt(dfoeDfieDfltArpt1,id);
                    ApplicationContextProvider.getBean(DfoeDfieService.class).InsertDfoeArpt(dfoeDfieDfltArpt2,id);
                    //DfoeDfieDflt执行插入语句
                    DfoeDfieDfltCkls ckls = dfoeDfie.getDflt().getCkls();
                    DfoeDfieDfltTmcd tmcd = dfoeDfie.getDflt().getJmcd();
                    ApplicationContextProvider.getBean(DfoeDfieService.class).InsertDfoeDfie(dfoeDfie.getDflt(),ckls,tmcd,id);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFOE-DFDL":
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
                    String id =sdf.format(System.currentTimeMillis());
                    InputStream inputStream = new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    DfoeDfdl dfoeDfdl= XmlUtils.parseXml(inputStream, DfoeDfdl.class);
                    log.info(dfoeDfdl+"");
                   //meta插入
                   ApplicationContextProvider.getBean(DfoeDfdlService.class).InsertDfoeMeta(dfoeDfdl.getMetaData(),id);
                    //插入dfoedfdl
                   ApplicationContextProvider.getBean(DfoeDfdlService.class).InsertDfoeDfdl(dfoeDfdl,id);
                    //插入dflt
                    for (int i = 0; i < dfoeDfdl.getDflt().size(); i++) {
                        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
                        String dfid =sdf1.format(System.currentTimeMillis());
                        dfid=dfid+"1";
                        log.info(dfid);
                        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
                        String cid =sdf2.format(System.currentTimeMillis());
                        //插入sflg
                        for (int i1 = 0; i1 < dfoeDfdl.getDflt().get(i).getSflg().size(); i1++) {
                            DfoeDfdlDfltSflg dfoeDfdlDfltSflg = dfoeDfdl.getDflt().get(i).getSflg().get(i1);
                            ApplicationContextProvider.getBean(DfoeDfdlService.class).InsertDfoeSflg(dfoeDfdlDfltSflg,dfid);
                        }
                        //插入arpt
                        for (int i2 = 0; i2 < dfoeDfdl.getDflt().get(i).getArpt().size(); i2++) {
                            DfoeDfdlDfltArpt dfoeDfdlDfltArpt = dfoeDfdl.getDflt().get(i).getArpt().get(i2);
                            if(dfoeDfdl.getDflt().get(i).getArpt().size()>=3){
                                switch (i2){
                                    case 0:
                                        ApplicationContextProvider.getBean(DfoeDfdlService.class).InsertDfoeArpt1(dfoeDfdlDfltArpt,dfid,cid);
                                        break;
                                    case 1:
                                        ApplicationContextProvider.getBean(DfoeDfdlService.class).InsertDfoeArpt2(dfoeDfdlDfltArpt,dfid,cid);
                                        break;
                                    case 2:
                                        ApplicationContextProvider.getBean(DfoeDfdlService.class).InsertDfoeArpt3(dfoeDfdlDfltArpt,dfid,cid);
                                        break;
                                }
                            }else{
                                switch (i2){
                                    case 0:
                                        ApplicationContextProvider.getBean(DfoeDfdlService.class).InsertDfoeArpt1(dfoeDfdlDfltArpt,dfid,cid);
                                        break;
                                    case 1:
                                        ApplicationContextProvider.getBean(DfoeDfdlService.class).InsertDfoeArpt3(dfoeDfdlDfltArpt,dfid,cid);
                                        break;
                                }
                            }

                        }
                        //插入dflt
                        DfoeDfdlDflt dfoeDfdlDflt = dfoeDfdl.getDflt().get(i);
                        ApplicationContextProvider.getBean(DfoeDfdlService.class).InsertDfoeDflt(dfoeDfdlDflt,dfoeDfdlDflt.getCkls(),dfoeDfdlDflt.getTmcd(),dfid,id);

                    }
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "DFME-ONRE":
                try {
                    //创建输入流
                    InputStream inputStream = new FileInputStream("D:\\files\\" + driction + "\\" + file.getName());
                    DfmeOnre dfmeOnre = XmlUtils.parseXml(inputStream, DfmeOnre.class);  //将xml文件转化为java对象(Msg类的实例)
                    ApplicationContextProvider.getBean(DfmeOnreDao.class).insertonre(dfmeOnre.getMeta(), dfmeOnre.getOnreDflt());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-LBDE":
                try {
                    //创建输入流
                    InputStream inputStream = new FileInputStream("D:\\files\\" + driction + "\\" + file.getName());
                    DfmeLbde dfmeLbde = XmlUtils.parseXml(inputStream, DfmeLbde.class);  //将xml文件转化为java对象(Msg类的实例)
                    ApplicationContextProvider.getBean(DfmeLbdeDao.class).insertlbde(dfmeLbde.getMeta(), dfmeLbde.getLbdeDflt());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            default:
                log.info("没有此文件");
        }
    }
    @Override
    public void onFileDelete(File file) {
        log.info("文件删除...");
    }

    @Override
    public void onStart(FileAlterationObserver fileAlterationObserver) {

        log.info("监听开始2...");
    }

    @Override
    public void onStop(FileAlterationObserver fileAlterationObserver) {

        log.info("监听结束2...");
    }
}
